<!--
 * @Author: ChunLai
 * @Date: 2022-03-29 17:06:31
 * @LastEditTime: 2025-09-02 15:40:18
 * @Description: 用户信息模块
 * @FilePath: \02.bldinsure\src\views\count\components\UserModule.vue
-->
<template>
  <div>
    <div
      class="bg-fff newModel"
      :class="{
        disEditModel: item.idNum,
        mb10: index != bbrMultList.length - 1,
      }"
      v-for="(item, index) in bbrMultList"
      :key="`bbr-pre${index}`"
    >
      <div class="u_head" v-if="bbrMultList.length > 1">
        <!-- 为谁投保 -->
        <span class="nickname">被保人{{ index + 1 }}</span>
        <MsImgIcon
          name="delete"
          class="ml20"
          @click.native="delUserItem(index)"
          v-if="index > 0"
        />
      </div>
      <van-field label="性别" input-align="right" readonly>
        <div slot="input">
          <MsRadios
            :arrList="sexList"
            :curVal="item.sex"
            type="sex"
            :uidx="index"
            @change="changeMsRadio"
          />
        </div>
      </van-field>
      <van-field label="出生日期" input-align="right" readonly>
        <div slot="input">
          <SelectAge
            :objData="item"
            :objIndex="index"
            :minDate="bbBirthMinDay"
            :maxDate="bbBirthMaxDay"
            @showTimes="showTimes"
          />
        </div>
      </van-field>
      <van-field label="有无社保" input-align="right" readonly>
        <div slot="input">
          <MsRadios
            :arrList="socialSecList"
            :curVal="item.socialInsuFlag"
            type="socialInsuFlag"
            :uidx="index"
            @change="changeMsRadio"
          />
        </div>
      </van-field>
      <van-field
        label="职业名称"
        input-align="right"
        :value="item.occupationCodeName"
        @click="showPopAction('job', 'occupationCode', [], index)"
        type="textarea"
        rows="1"
        autosize
        readonly
        placeholder="请选择职业"
      >
        <span class="ms-row-icon" slot="right-icon"></span>
      </van-field>
    </div>
    <div class="rule-warn-msg" v-html="ruleWarn" v-if="showRuleWarn"></div>
    <!-- 投保人信息 - 选择豁免显示 -->
    <div v-if="showAppntMode">
      <div class="u_head">投保人</div>
      <div class="bg-fff">
        <van-field label="性别" input-align="right" readonly>
          <div slot="input">
            <MsRadios
              :arrList="sexList"
              :curVal="applicant.sex"
              type="tbr-sex"
              @change="changeMsRadio"
            />
          </div>
        </van-field>
        <van-field label="出生日期" input-align="right" readonly>
          <div slot="input">
            <SelectAge
              :objData="applicant"
              :minDate="tbBirthMin"
              :maxDate="tbBirthMax"
              objType="tb"
              @showTimes="showTimes"
            />
          </div>
        </van-field>
      </div>
    </div>
    <div class="addCell" @click="addUserItem()" v-if="addMoreBtn">
      <MsImgIcon name="add" />
      <span class="ft15 ml4 lh_20">添加被保人</span>
    </div>
    <van-popup v-model="showPop" round position="bottom">
      <OccupationPop @cancel="showPop = false" @confirm="onConfirmDJob" />
    </van-popup>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import SelectAge from "./SelectAge";
import { dateToString } from "@/methods/date.js";

export default {
  name: "UserModule",
  data() {
    return {
      showPop: false,
      popConfig: {},
      index: 0,
    };
  },
  components: {
    SelectAge,
  },
  computed: {
    ...mapGetters({
      applicant: "insert/applicant",
      bbrMultList: "insert/bbrMultList",
      bbrMaxNum: "page/bbrMaxNum",
      ruleWarn: "page/ruleWarn",
      ageRange: "infoReg/ageRange",
      tbBirthMin: "infoReg/tbBirthMin",
      tbBirthMax: "infoReg/tbBirthMax",
      bbBirthMin: "infoReg/bbBirthMin",
      bbBirthMax: "infoReg/bbBirthMax",
      sexList: "dictionary/sexList",
      socialSecList: "dictionary/socialSecList",
      planModule: "home/planModule",
      mainInsu_e: "risks/mainInsu",
      subRisk: "risks/subRisk",
    }),
    showAppntMode() {
      let res = false;
      if (this.subRisk.length > 0) {
        const fdObj = this.subRisk.find(
          (ele) => ele.productName.indexOf("投保人豁免") != -1
        );
        if (fdObj) {
          res = true;
        }
      }
      return res;
    },
    hasSexCheckAge() {
      let res = null;
      const mainObj = this.mainInsu_e[0],
        ruleCfg = mainObj.rules || [],
        ruleObj = ruleCfg.find((ele) => ele.type == "sexLimitMaxAge");
      if (ruleObj) {
        res = ruleObj;
      }
      return res;
    },
    sameApplicant() {
      let res = false;
      // res= this.bbrMultList[0].relationToAppnt == "4"
      return res;
    },
    getTbrMinAge() {
      let res = 0;
      const obj = this.ageRange.applicant || {};
      if (obj.minAge) {
        res = obj.minAge;
      }
      return res;
    },
    getBbrMinAge() {
      let res = 0;
      const obj = this.ageRange.insured || {};
      if (obj.minAge && !obj.isDate) {
        res = obj.minAge;
      }
      return res;
    },
    bbBirthMinDay() {
      if (this.sameApplicant) {
        return this.bbBirthMin;
      }
      return this.bbBirthMin;
    },
    bbBirthMaxDay() {
      let res = this.bbBirthMax;
      if (this.sameApplicant && this.getTbrMinAge > this.getBbrMinAge) {
        res = this.tbBirthMax;
      }
      return res;
    },
    isureAgeTip() {
      const obj2 = this.planModule.find((ele) => ele.label === "投保年龄");
      return obj2 ? obj2.value : "";
    },
    showRuleWarn() {
      let res = false;
      if (
        this.ruleWarn &&
        this.bbrMultList.length == 2 &&
        this.mainRiskCode == "110150"
      ) {
        res = true;
      }
      return res;
    },
    addMoreBtn() {
      let res = true;
      const numLen = this.bbrMultList.length;
      if (numLen >= this.bbrMaxNum) {
        res = false;
      }
      return res;
    },
  },
  created() {
    this.initConfig();
  },
  mounted() {},
  methods: {
    /**初始化配置信息 */
    initConfig() {
      if (!this.applicant.idNum) {
        let tbrdate = this.tbBirthMax;
        this.$store.commit("insert/UPDATE_APPLICANT", {
          birthDate: dateToString(tbrdate),
          age: this.$GlobalFuns.userAge(dateToString(tbrdate)),
        });
      }
      if (!this.bbrMultList[0].idNum) {
        let bbrdate = this.bbBirthMaxDay;
        let ageConfig = {
          birthDate: dateToString(bbrdate),
          age: this.$GlobalFuns.userAge(dateToString(bbrdate)),
          relationToAppnt: "",
          relationToAppntName: "",
        };
        this.$store.commit("insert/UPDATE_MULT_BBR", {
          data: ageConfig,
          index: 0,
        });
      }
      this.$store.dispatch("risks/setRisks", {});
    },
    onConfirmDJob(val) {
      const str1 = val.accClass,
        str2 = this.levelName || "类",
        leiming = str1 !== void 0 && str1 !== "" ? `(${str1}${str2})` : "";
      let data = {
        occupationCode: val.value,
        occupationCodeName: val.name + `${leiming}`,
        occupationClass: val.accClass,
      };
      this.$store.commit("insert/UPDATE_MULT_BBR", {
        data: data,
        index: this.index,
      });
      if (this.sameApplicant) {
        this.$store.commit("insert/UPDATE_APPLICANT", data);
      }
      this.$store.dispatch("risks/setRisks", {});
    },
    showPopAction(type, key, list, index) {
      this.popConfig = {
        type,
        key,
        list,
      };
      this.index = index;
      this.showPop = true;
    },
    showTimes(objVal, type, index = 0) {
      if (type === "tb") {
        this.$store.commit("insert/UPDATE_APPLICANT", objVal);
      } else {
        this.$store.commit("insert/UPDATE_MULT_BBR", {
          data: objVal,
          index: index,
        });
        if (this.sameApplicant) {
          this.$store.commit("insert/UPDATE_APPLICANT", objVal);
        }
      }
      this.$store.dispatch("risks/setRisks", {});
    },
    changeMsRadio(btnType, obj, index) {
      // console.log(obj, btnType);
      if (btnType == "tbr-sex") {
        this.$store.commit("insert/UPDATE_APPLICANT", {
          sex: obj.value,
        });
      } else if (btnType == "sex") {
        // 主险性别限制年龄校验规则(长城/00369)
        if (this.hasSexCheckAge) {
          const usex = obj.value,
            cfgObj = this.hasSexCheckAge.config.find((ele) => ele.sex == usex);
          if (cfgObj) {
            let errTip = "",
              errStr = `被保险人为${usex == "1" ? "女性" : "男性"}`;
            if (cfgObj.max && this.bbrMultList[0].age > cfgObj.max) {
              errTip = `${errStr}最大投保年龄为${cfgObj.max}岁`;
            }
            if (cfgObj.min && this.bbrMultList[0].age < cfgObj.min) {
              errTip = `${errStr}最小投保年龄为${cfgObj.min}岁`;
            }
            if (errTip) {
              return this.$toast(errTip);
            }
          }
        }
        this.$store.commit("insert/UPDATE_MULT_BBR", {
          data: { sex: obj.value },
          index: index,
        });
        if (this.sameApplicant) {
          this.$store.commit("insert/UPDATE_APPLICANT", { sex: obj.value });
        }
      } else if (btnType == "socialInsuFlag") {
        let upobj = {
          socialInsuFlag: obj.value,
          socialInsuFlagName: obj.label,
        };
        this.$store.commit("insert/UPDATE_MULT_BBR", {
          data: upobj,
          index: index,
        });
        if (this.sameApplicant) {
          this.$store.commit("insert/UPDATE_APPLICANT", upobj);
        }
      }
      this.$store.dispatch("risks/setRisks", {});
    },
    addUserItem() {
      // 选计划1不能加附加被保人
      const zhuRisk = this.mainInsu_e[0];
      if (
        ["110176", "110194"].includes(zhuRisk.varietyCode) &&
        zhuRisk.planCode === "1"
      ) {
        this.$store.commit("common/SET_FIELD_MEAN_CONT", {
          label: `规则校验`,
          desc: `${zhuRisk.productName}：选计划1不能添加附加被保人。`,
        });
        return;
      }
      this.$store.dispatch("insert/addNewBbr");
    },
    delUserItem(index) {
      this.$baseConfirm("是否要删除该被保人?", () => {
        this.$store.commit("insert/DELETE_MULT_BBR", index);
      });
    },
  },
};
</script>

<style lang="less" scoped>
.u_head {
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  font-size: 16px;
  line-height: 1;
  color: #242424;
  .nickname {
    font-size: 14px;
    color: #606266;
    font-family: MiSans;
    padding-left: 10px;
    position: relative;
    &::before {
      content: "";
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      margin: auto;
      width: 5px;
      height: 5px;
      border-radius: 100%;
      background-color: #f54040;
    }
  }
}

.addCell {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 15px;
  background: #ffffff;
  color: #43cf7c;
  margin-top: 10px;
}
</style>
